#include "nameMenu.h"
#include <iostream>
#include <ncurses.h>
using namespace std;
//nameMenu--------------------------------------------------
nameMenu::nameMenu(BaseObjectType* obj, const Glib::RefPtr<Gtk::Builder>& window) : Gtk::DrawingArea(obj) {
    set_can_focus(true);
    offset = 0;
    choice = 0;
}
nameMenu::nameMenu() {
    set_can_focus();
    offset = 0;
    choice = 0;
}
nameMenu::~nameMenu() {
}
void nameMenu::addNames(string newstr) {
    cout << " adding " << newstr << endl;
    names.push_back(new string(newstr));
}
bool nameMenu::on_button_press(GdkEventButton* btn) {
}
bool nameMenu::on_key_press_event(GdkEventKey* event) {
    cout << "yes? n-s-" << names.size() << " k-" << event->keyval << endl;
    switch (event->keyval) {
        case GDK_Up:
        case GDK_Left:
            choice--;
            if (choice < 0)
                choice = 0;
            break;
        case GDK_Down:
        case GDK_Right:
            choice++;
            if (choice >= names.size())
                choice = names.size() - 1;
            break;
        case GDK_Return:
        case 65421:
            entersig.emit(gChoice());
            return true;
            break;
    }
    draw();
    return true;
}
sigc::signal<void, string>* nameMenu::enterSignal() {
    return &entersig;
}
void nameMenu::loadDirectory(string dirname, string ft) {
    DIR* herp;
    dirent* derp;
    struct stat derps;
    string str;
    herp = opendir(dirname.c_str());
    fileType = ft;
    fileHeader = dirname;
    if(herp == NULL) return;
    while(derp = readdir(herp)){
        str = dirname + "/" + derp->d_name;
        stat(str.c_str(), &derps);
        if(S_ISDIR(derps.st_mode)) continue;
        if(str.find(fileType) != str.length()){
            str = string(derp->d_name);
            str = str.substr(0, str.find(fileType));
            addNames(str);
        }
    }
}
string nameMenu::gChoice() {
    return fileHeader + "/" + (*names[choice] + fileType);
}
bool nameMenu::on_expose_event(GdkEventExpose *event) {
    draw();
}
void nameMenu::keypress(GdkEventKey* event) {
    on_key_press_event(event);
}
void nameMenu::draw() {
    //grab the window
    Glib::RefPtr<Gdk::Window> window = get_window();
    Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
    //grab the theme

    if (window) { //window not null
        //save cairo state;
        cr->save();

        //window size info
        Gtk::Allocation allocation = get_allocation();
        const int width = allocation.get_width();
        const int height = allocation.get_height();

        //window locations
        int center_x = width / 2;
        int center_y = height / 2;
        int leftBind = 0 + LEFT_BORDER;
        int rightBind = width - RIGHT_BORDER;
        int topBind = 0 + TOP_BORDER;
        int botBind = height - BOTTOM_BORDER;

        //font information
        cr->set_source_rgb(0, 0, 0);
        cr->select_font_face("monospace", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD);
        Cairo::TextExtents sizeing;
        cr->get_text_extents("TEST", sizeing);
        int fontSpacing = sizeing.height * 2;
        int strsToPrint = MIN((botBind - topBind) / fontSpacing, names.size());
        //draw background
        cr->save();
        cr->set_fill_rule(Cairo::FILL_RULE_EVEN_ODD);
        cr->set_source_rgb(.05, .05, .05);
        cr->rectangle(0, 0, width, height);
        cr->fill();
        cr->restore();
        //draw background
        cr->save();
        cr->set_fill_rule(Cairo::FILL_RULE_WINDING);
        cr->set_source_rgb(.7, .7, .7);
        cr->rectangle(leftBind, topBind, rightBind - leftBind, botBind - topBind);
        cr->fill();
        cr->restore();

        //start drawing strings
        int line = topBind + fontSpacing;
        for (int i = offset; i < strsToPrint; i++) {
            //prepare information
            cr->get_text_extents(*names[i], sizeing);
            //draw selection bar
            if (i == choice) {
                cr->save();
                cr->move_to(leftBind, line);
                cr->set_source_rgb(1, 1, 1);
                cr->rectangle(leftBind, line - sizeing.height - 2, rightBind - leftBind, sizeing.height + 4);
                cr->fill();
                cr->restore();
            }
            cr->move_to(leftBind + 20, line);
            cr->show_text(*names[i]);
            line += fontSpacing;
        }

        //restore cairo state
        cr->restore();
    }
}
//----------------------------------------------------------
